const base64_to_json = function (content) {

    if (content === undefined) {
        return {};
    }

    if (typeof content === 'object') {
        return content;
    }

    if (typeof content === 'string' && !content.length) {
        return {};
    }

    let data = {};
    try {
        const binaryStr = atob(content);
        const bytes = new Uint8Array(binaryStr.length);
        for (let i = 0; i < binaryStr.length; i++) {
            bytes[i] = binaryStr.charCodeAt(i);
        }
        data = new TextDecoder('utf-8').decode(bytes);
        if (typeof data === 'string') {
            data = JSON.parse(data);
        }
    } catch (e) {
        console.log(content);
        console.log('show:base64_to_json error')
        console.log(e);
        data = {};
    }
    return data;
}

const json_to_base64 = function (content) {
    if (typeof content !== 'object' || Object.keys(content).length === 0) {
        return '';
    }
    content = JSON.stringify(content);
    const bytes = new TextEncoder().encode(content);
    const binaryStr = String.fromCharCode(...bytes);
    return btoa(binaryStr);
}

const generate_random_str = function (length = 16) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
}

/**
 * 伪uuid
 * @returns {`${string}-${string}-${string}-${string}-${string}`}
 */
const generate_unique_id = function () {
    const random_part1 = Math.random().toString(16).slice(2, 10);
    const random_part2 = Math.random().toString(16).slice(2, 6);
    const random_part3 = Math.random().toString(16).slice(2, 6);
    const random_part4 = Math.random().toString(16).slice(2, 6);
    const random_part5 = Math.random().toString(16).slice(2, 14);
    return `${random_part1}-${random_part2}-${random_part3}-${random_part4}-${random_part5}`;
}

layui.use(function () {

    let laydate = layui.laydate

    // 绑定时间
    $('[data-date]').each(function () {
        let id = $(this).attr('id')
        let type = $(this).data('format')
        let range = $(this).data('range')
        laydate.render({
            elem: '#' + id,
            type: type,
            range: range ? '~' : false,
            rangeLinked: range ? true : false,
            calendar: true,
        })
    })
})